import vue from 'vue'
import Vuex from 'vuex'
vue.use(Vuex);
const state = {
    blogTitle: '我是vuex测试',
    views: 10,
    blogNumber: 100,
    total: 0,
    todos: [
        { id: 1, done: true, text: '我是码农' },

        { id: 2, done: false, text: '我是码农202号' },

        { id: 3, done: true, text: '我是码农202号' }
    ]
}
const mutations = {

    addViews(state) {

        state.views++

    },

    blogAdd(state) {

        state.blogNumber++

    },

    clickTotal(state, payload) {
        // axios.get('https://api.apiopen.top/getJoke?page=1&count=2&type=video').then((res) => {
        //     let result = res.data;
        //     if (result.code == '200') {
        //         state.total += payload.num
        //     }
        // })
        // setTimeout(function() {
        //         state.total ++
        //     }, 5000)
        state.total += payload.num

    }

}
const actions = {

    addViews({ commit }) {

        commit('addViews')

    },

    clickTotal(context, payload) {
        console.log(payload)
        setTimeout(function() {
            context.commit('clickTotal', { num: payload.num })
            payload.success()
        }, 5000)


    },

    blogAdd({ commit }) {

        commit('blogAdd')

    }

}
const getters = {

    getToDo(state,getters,rootState) {

        return state.todos.filter(item => item.done === true)

        // filter 迭代过滤器 将每个item的值 item.done == true 挑出来， 返回的是一个数组

    }

}
export default new Vuex.Store({

    state,

    actions,

    mutations,

    getters

})